package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.common.exception.PasswordErrorException;
import com.usian.common.exception.UsernameNotExistException;
import com.usian.mapper.AdminMapper;
import com.usian.pojo.Admin;
import com.usian.pojo.vo.LoginReqVo;
import com.usian.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2025 02 26 14 58
 **/

@Service
@Slf4j
public class AdminServiceImpl
extends ServiceImpl<AdminMapper, Admin>
implements AdminService {


    @Override
    public Result login(LoginReqVo loginReqVo) throws Exception {
        log.debug("login==============================");

        String username = loginReqVo.getUsername();
        String password = loginReqVo.getPassword();


        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.lambda().eq(Admin::getUsername,username);

        Admin admin = this.baseMapper.selectOne(adminQueryWrapper);
        if (null==admin){
            throw new UsernameNotExistException(ResultCodeEnum.USERNAME_NOT_EXIST);

        }else{
            if (admin.getPassword().equals(password)){
                return Result.ok(admin);
            }else{
                throw new PasswordErrorException(ResultCodeEnum.PASSWORD_ERROR);

            }
        }

    }
}
